package com.banciyuan.bcywebview.biz.detail.comment;

import android.content.Context;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.api.CoserApi;
import com.banciyuan.bcywebview.api.DailyApi;
import com.banciyuan.bcywebview.api.GroupApi;
import com.banciyuan.bcywebview.api.IllustApi;
import com.banciyuan.bcywebview.api.NovelApi;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.DetailType;

/**
 * Created by squall on 2016/3/18.
 */
public class ZanCommentHelper {

    private IZanComment iZanComment;
    private RequestQueue mQueue;
    private Context mContext;

    public ZanCommentHelper(IZanComment iZanComment, Context mContext) {
        this.mContext = mContext;
        this.iZanComment = iZanComment;
        mQueue = VolleyQueue.getRquest(mContext);
    }


    public void doPraiseComment(DetailType mDetailType, String comment_id) {
        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, mContext)) {
                    iZanComment.priseSucc(s);
                } else {
                    iZanComment.priseFail();
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                iZanComment.priseFail();
            }
        };

        List<BcyPair> paramsList = new ArrayList<BcyPair>();
        String url = "";

        switch (mDetailType.getTimelineType()) {
            case TimelineFactory.TIMELINE_TYPE_ILLUST:
            case TimelineFactory.TIMELINE_TYPE_COSER:
                paramsList.add(new BcyPair(HttpUtils.RPID, mDetailType.getRp_id()));
                if (TextUtils.isEmpty(mDetailType.getCp_id())) {
                    paramsList.add(new BcyPair(HttpUtils.DPID, mDetailType.getDp_id()));
                    url = HttpUtils.BASE_URL + IllustApi.getDoreplyzan();
                } else {
                    paramsList.add(new BcyPair(HttpUtils.CPID, mDetailType.getCp_id()));
                    url = HttpUtils.BASE_URL + CoserApi.getDoreplyzan();
                }
                break;
            case TimelineFactory.TIMELINE_TYPE_DAILY:
                paramsList.add(new BcyPair(HttpUtils.UDID, mDetailType.getUd_id()));
                url = HttpUtils.BASE_URL + DailyApi.getDoreplyding();
                break;
            case TimelineFactory.TIMELINE_TYPE_GROUP:
                paramsList.add(new BcyPair(HttpUtils.POSTID, mDetailType.getPost_id()));
                paramsList.add(new BcyPair(HttpUtils.GID, mDetailType.getGid()));
                url = HttpUtils.BASE_URL + GroupApi.getDoreplyding();
                break;
            case TimelineFactory.TIMELINE_TYPE_WRITER:

                paramsList.add(new BcyPair(HttpUtils.WPID, mDetailType.getWp_id()));
                paramsList.add(new BcyPair(HttpUtils.RPID, mDetailType.getRp_id()));
                url = HttpUtils.BASE_URL + NovelApi.getDoreplyzan();
                break;
            default:
                break;
        }
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        paramsList.add(new BcyPair(HttpUtils.COMMENT_ID, comment_id));
        Map<String, String> map = HttpUtils.getData(paramsList);

        StringRequestParam getUserdataRequest = new StringRequestParam(Request.Method.POST, url, map, succ, error);
        mQueue.add(getUserdataRequest);
    }

    public void unDoPraiseComment(DetailType mDetailType, String comment_id) {
        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, mContext)) {
                    iZanComment.unPriseSucc(s);
                } else {
                    iZanComment.unPriseFail();
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                iZanComment.unPriseFail();
            }
        };
        List<BcyPair> paramsList = new ArrayList<BcyPair>();
        String url = "";

        switch (mDetailType.getTimelineType()) {
            case TimelineFactory.TIMELINE_TYPE_ILLUST:
            case TimelineFactory.TIMELINE_TYPE_COSER:
                paramsList.add(new BcyPair(HttpUtils.RPID, mDetailType.getRp_id()));
                if (TextUtils.isEmpty(mDetailType.getCp_id())) {
                    paramsList.add(new BcyPair(HttpUtils.DPID, mDetailType.getDp_id()));
                    url = HttpUtils.BASE_URL + IllustApi.getunDoreplyzan();
                } else {
                    paramsList.add(new BcyPair(HttpUtils.CPID, mDetailType.getCp_id()));
                    url = HttpUtils.BASE_URL + CoserApi.getunDoreplyzan();
                }
                break;
            case TimelineFactory.TIMELINE_TYPE_DAILY:
                paramsList.add(new BcyPair(HttpUtils.UDID, mDetailType.getUd_id()));
                url = HttpUtils.BASE_URL + DailyApi.getunDoreplyding();
                break;
            case TimelineFactory.TIMELINE_TYPE_GROUP:
                paramsList.add(new BcyPair(HttpUtils.POSTID, mDetailType.getPost_id()));
                paramsList.add(new BcyPair(HttpUtils.GID, mDetailType.getGid()));
                url = HttpUtils.BASE_URL + GroupApi.getunDoreplyding();
                break;
            case TimelineFactory.TIMELINE_TYPE_WRITER:

                paramsList.add(new BcyPair(HttpUtils.WPID, mDetailType.getWp_id()));
                paramsList.add(new BcyPair(HttpUtils.RPID, mDetailType.getRp_id()));
                url = HttpUtils.BASE_URL + NovelApi.getunDoreplyzan();
                break;
            default:
                break;
        }
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        paramsList.add(new BcyPair(HttpUtils.COMMENT_ID, comment_id));

        Map<String, String> map = HttpUtils.getData(paramsList);

        StringRequestParam getUserdataRequest = new StringRequestParam(Request.Method.POST, url, map, succ, error);
        mQueue.add(getUserdataRequest);
    }

    public void doPraiseRepley(DetailType mDetailType, String reply_id, String comment_Id) {
        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, mContext)) {
                    iZanComment.priseSucc(s);
                } else {
                    iZanComment.priseFail();
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                iZanComment.priseFail();
            }
        };
        List<BcyPair> paramsList = new ArrayList<BcyPair>();
        String url = "";

        switch (mDetailType.getTimelineType()) {
            case TimelineFactory.TIMELINE_TYPE_ILLUST:
            case TimelineFactory.TIMELINE_TYPE_COSER:
                paramsList.add(new BcyPair(HttpUtils.RPID, mDetailType.getRp_id()));
                if (TextUtils.isEmpty(mDetailType.getCp_id())) {
                    paramsList.add(new BcyPair(HttpUtils.DPID, mDetailType.getDp_id()));
                    url = HttpUtils.BASE_URL + IllustApi.getDoReplyCommentZan();
                } else {
                    paramsList.add(new BcyPair(HttpUtils.CPID, mDetailType.getCp_id()));
                    url = HttpUtils.BASE_URL + CoserApi.getDoReplyCommentZan();
                }
                break;
            case TimelineFactory.TIMELINE_TYPE_DAILY:
                paramsList.add(new BcyPair(HttpUtils.UDID, mDetailType.getUd_id()));
                url = HttpUtils.BASE_URL + DailyApi.getDoReplyCommentZan();
                break;
            case TimelineFactory.TIMELINE_TYPE_GROUP:
                paramsList.add(new BcyPair(HttpUtils.POSTID, mDetailType.getPost_id()));
                paramsList.add(new BcyPair(HttpUtils.GID, mDetailType.getGid()));
                url = HttpUtils.BASE_URL + GroupApi.getDoReplyCommentZan();
                break;
            case TimelineFactory.TIMELINE_TYPE_WRITER:

                paramsList.add(new BcyPair(HttpUtils.WPID, mDetailType.getWp_id()));
                paramsList.add(new BcyPair(HttpUtils.RPID, mDetailType.getRp_id()));
                url = HttpUtils.BASE_URL + NovelApi.getDoReplyCommentZan();
                break;
            default:
                break;
        }
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        paramsList.add(new BcyPair(HttpUtils.REPLY_ID, reply_id));
        paramsList.add(new BcyPair(HttpUtils.COMMENT_ID, comment_Id));

        Map<String, String> map = HttpUtils.getData(paramsList);

        StringRequestParam getUserdataRequest = new StringRequestParam(Request.Method.POST, url, map, succ, error);
        mQueue.add(getUserdataRequest);
    }

    public void unDoPraiseRepley(DetailType mDetailType, String reply_id, String comment_id) {
        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (StatusCodeUtils.dealStatusCode(s, mContext)) {
                    iZanComment.unPriseSucc(s);
                } else {
                    iZanComment.unPriseFail();
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                iZanComment.unPriseFail();
            }
        };
        List<BcyPair> paramsList = new ArrayList<BcyPair>();
        String url = "";

        switch (mDetailType.getTimelineType()) {
            case TimelineFactory.TIMELINE_TYPE_ILLUST:
            case TimelineFactory.TIMELINE_TYPE_COSER:
                paramsList.add(new BcyPair(HttpUtils.RPID, mDetailType.getRp_id()));
                if (TextUtils.isEmpty(mDetailType.getCp_id())) {
                    paramsList.add(new BcyPair(HttpUtils.DPID, mDetailType.getDp_id()));
                    url = HttpUtils.BASE_URL + IllustApi.getUnDoReplyCommentZan();
                } else {
                    paramsList.add(new BcyPair(HttpUtils.CPID, mDetailType.getCp_id()));
                    url = HttpUtils.BASE_URL + CoserApi.getUnDoReplyCommentZan();
                }
                break;
            case TimelineFactory.TIMELINE_TYPE_DAILY:
                paramsList.add(new BcyPair(HttpUtils.UDID, mDetailType.getUd_id()));
                url = HttpUtils.BASE_URL + DailyApi.getUnDoReplyCommentZan();
                break;
            case TimelineFactory.TIMELINE_TYPE_GROUP:
                paramsList.add(new BcyPair(HttpUtils.POSTID, mDetailType.getPost_id()));
                paramsList.add(new BcyPair(HttpUtils.GID, mDetailType.getGid()));
                url = HttpUtils.BASE_URL + GroupApi.getUnDoReplyCommentZan();
                break;
            case TimelineFactory.TIMELINE_TYPE_WRITER:

                paramsList.add(new BcyPair(HttpUtils.WPID, mDetailType.getWp_id()));
                paramsList.add(new BcyPair(HttpUtils.RPID, mDetailType.getRp_id()));
                url = HttpUtils.BASE_URL + NovelApi.getUnDoReplyCommentZan();
                break;
            default:
                break;
        }
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        paramsList.add(new BcyPair(HttpUtils.REPLY_ID, reply_id));
        paramsList.add(new BcyPair(HttpUtils.COMMENT_ID, comment_id));

        Map<String, String> map = HttpUtils.getData(paramsList);

        StringRequestParam getUserdataRequest = new StringRequestParam(Request.Method.POST, url, map, succ, error);
        mQueue.add(getUserdataRequest);
    }

}
